-- 新机制：技力
local charge = fk.CreateSkill {
    name = "mcdh_charge",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

--回复技力
charge:addEffect(fk.TurnEnd, {
    global = true,
    late_refresh = true,
    can_refresh = function(self, event, target, player, data)
        if player:getMark("mcdh_charge") < player:getMark("mcdh_charge_max") then
            return  #player.room.logic:getActualDamageEvents(1, function (e)
                return true
            end, Player.HistoryTurn) > 0
        end
    end,
    on_refresh= function(self, event, target, player, data)
        lib.mcdh_charge(player, 1)
    end,
})

--初始化武将技力
charge:addEffect(fk.AfterPropertyChange,{
    global = true,
    can_refresh = function(self, event, target, player, data)
        local general = Fk.generals[player.general]
        local to = data.from
        return data.general and data.general ~= "" and data.results.generalChange and player == to
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local general = Fk.generals[player.general]
        if general.maxCharge then
            local now_max_charge = player:getMark("mcdh_charge_max")
            lib.mcdh_charge(player, 0,general.maxCharge-now_max_charge )
        else
            room:setPlayerMark(player, "mcdh_charge_max",0)
            room:setPlayerMark(player, "mcdh_charge", 0)
            room:setPlayerMark(player, "@mcdh_charge", 0)
        end
        if general.charge then
            local now_charge = player:getMark("mcdh_charge")
            lib.mcdh_charge(player, general.charge-now_charge)
        end
    end
})

charge:addEffect(fk.GamePrepared,{
    global = true,
    can_refresh = function(self, event, target, player, data)
        return true
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local general = Fk.generals[player.general]
        if general.maxCharge then
            local now_max_charge = player:getMark("mcdh_charge_max")
            lib.mcdh_charge(player, 0,general.maxCharge-now_max_charge )
        else
            room:setPlayerMark(player, "mcdh_charge_max",0)
            room:setPlayerMark(player, "mcdh_charge", 0)
            room:setPlayerMark(player, "@mcdh_charge", 0)
        end
        if general.charge then
            local now_charge = player:getMark("mcdh_charge")
            lib.mcdh_charge(player, general.charge-now_charge)
        end
    end
})


Fk:loadTranslationTable {
    ["mcdh_charged_skill"] = "蓄力",
    ["charge_skill"] = "每回合结束后，若本回合有角色造成或受到过伤害，所有角色获得1点技力。" ..
            "<font color='grey'><small>" ..
            "<br>当一名角色发动带有“<b>蓄力技(n)</b>”的技能时，将消耗n点“蓄力”值。" ..
            "<br>如果“蓄力”值小于n，则不能发动该蓄力技。" ..
            "<br>若n为“X”，且后续技能描述中没有为X赋值，则角色发动技能时，X为玩家声明的一个任意正整数。" ..
            "</small></font>",
}

return charge